package com.lcry.springcloudalibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lcry.springcloud.entities.CommonResult;
import com.lcry.springcloud.entities.Payment;
import com.lcry.springcloudalibaba.myhandler.CustomerBlockHandler;
import com.lcry.springcloudalibaba.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * CircleBreakerController
 *
 * @author lcry
 * @date 2020/03/17 16:18
 * 使用示例：
 */
@RestController
@Slf4j
public class CircleBreakerController {
    public static final String SERVICE_URL = "http://nacos-payment-provider";

    //    整个rabbin - restTemplate
    @Resource
    private RestTemplate restTemplate;

    //    整合sentinel
    @RequestMapping("/consumer/fallback/{id}")
    //@SentinelResource(value = "fallback") //没有配置
    //@SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback只负责业务异常
    //@SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler只负责sentinel控制台配置违规
//    @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler",  //分开自定义
//            exceptionsToIgnore = {IllegalArgumentException.class}//排除异常不限流
//            )
    @SentinelResource(value = "fallback", //,fallback = "handlerFallback",
            blockHandlerClass = CustomerBlockHandler.class,  //异常自定义
            blockHandler = "handlerException1"  //分开自定义
    )
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);

        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }

    //本例是fallback
    public CommonResult handlerFallback(@PathVariable Long id, Throwable e) {
        Payment payment = new Payment(id, "null");
        return new CommonResult<>(444, "兜底异常handlerFallback,exception内容  " + e.getMessage(), payment);
    }

    //本例是blockHandler
    public CommonResult blockHandler(@PathVariable Long id, BlockException blockException) {
        Payment payment = new Payment(id, "null");
        return new CommonResult<>(445, "blockHandler-sentinel限流,无此流水: blockException  " + blockException.getMessage(), payment);
    }

    //OpenFeign调用
    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/consumer/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
        return paymentService.paymentSQL(id);
    }


    /*
   测试自定义限流
     */
    @GetMapping(value = "/consumer/paymentSQL")
    @SentinelResource(value = "customerBlockHandler",     //资源名称
            blockHandlerClass = CustomerBlockHandler.class,  //异常自定义
            blockHandler = "handlerException1")   //调用自定义类某个方法
    public CommonResult<Payment> paymentSQL2() {
        return paymentService.paymentSQL(1L);
    }
}

